<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	$GLOBALS['OUTPUT']->master_section = 'management';
	$GLOBALS['OUTPUT']->minor_section = $GLOBALS['LANG']->lang_admin->managegroups;
	$GLOBALS['OUTPUT']->cpmenu = $menus[$GLOBALS['OUTPUT']->master_section];
	
	$groups = array();
	
	$del = x7in::read('del','i');
	if(!empty($del)){
		$test = new lib_group($del);
		if($del <= 3){
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->cannotdeletegroup;
		}elseif($test->is_valid()){
			// Ensure that this group is empty
			$riterator = $GLOBALS['DATABASE']->get('users');
			while($udata = $riterator->get_next()){
				if($udata['group'] == $del){
					$notempty = true;
					break;
				}
			}
			
			if(!isset($notempty)){
				$test->remove();
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->groupdelete;
			}else{
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->groupnotempty;
			}
		}
	}
	
	// Obtain a list of groups
	$groups = array();
	$riterator = $GLOBALS['DATABASE']->get('groups');
	while($gdata = $riterator->get_next()){
		$groups[$gdata['name']] = $gdata['gid'];
	}
	
	$add = x7in::read('groupname');
	if(!empty($add)){
		$img = x7in::read('groupimage');
		
		if(isset($groups[$add])){
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->groupnameinuse;
		}else{
			$gid = create_group($add,$img);
			$groups[$add] = $gid;
			$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_admin->groupcreated;
		}
		
	}
	
	$GLOBALS['OUTPUT']->groups = $groups;
	
	echo tpl_managegroups::managegroups();
?>